import { legacy_createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import logger from "redux-logger";
import "../api/data"
import { ALLACTION, ADDADDRESS,EDITADDRESS, DELADDRESS, GETADDRESS, ALLSTATE, CHANGECHECK, CHECKALL, ADDNUM, REDUCENUM, CHECKONE, GETLIST, SAVEINDEX, GETIMGS, GETCLASSIFY, GETSHOPCAR } from "../types/store.d"
const initialState = {
    imgsArr: JSON.parse(window.localStorage.getItem('imgsArr') as string) || [],
    listArr: JSON.parse(window.localStorage.getItem('listArr') as string) || [],
    classifyArr: JSON.parse(window.localStorage.getItem('classifyArr') as string) || [],
    shopcarArr: JSON.parse(window.localStorage.getItem('shopcarArr') as string) || [],
    rightArr: JSON.parse(window.localStorage.getItem('rightArr') as string) || [],
    checkbox: JSON.parse(window.localStorage.getItem('checkbox') as string) || false,
    allmoney: JSON.parse(window.localStorage.getItem('allmoney') as string) || 0,
    changechecked: JSON.parse(window.localStorage.getItem('changecheck') as string) || false,
    addressArr: []
}

const reducer = (state: ALLSTATE = initialState, { type, payload }: ALLACTION) => {
    const allmoney = () => {
        let allArr = [...state.shopcarArr];
        //    let allmoney=[...state.allmoney];
        let all = 0
        allArr.forEach((item, index) => {
            if (item.flag) {
                all += item.num * item.price
            }
        })
        state.allmoney = all;
        window.localStorage.setItem('allmoney', JSON.stringify(state.allmoney))
    }
    switch (type) {
        case GETIMGS:
            let newArr = [...state.imgsArr]
            newArr = payload;
            state.imgsArr = newArr;
            window.localStorage.setItem('imgsArr', JSON.stringify(state.imgsArr))
            return { ...state }
        case GETLIST:
            let Arr = [...state.listArr]
            Arr = payload;
            state.listArr = Arr;
            window.localStorage.setItem('listArr', JSON.stringify(state.listArr))
            return { ...state }
        case GETCLASSIFY:
            let ARR = [...state.classifyArr]
            ARR = payload;
            state.classifyArr = ARR;
            state.rightArr = ARR[0].info as any
            window.localStorage.setItem('classifyArr', JSON.stringify(state.classifyArr))
            return { ...state }
        case GETSHOPCAR:
            let arr = [...state.shopcarArr]
            let isFlag = arr.some((item, index) => {
                return item.id === payload.id
            })
            if (isFlag) {
                arr.forEach((item, index) => {
                    if (item.id === payload.id) {
                        item.num++
                    }
                })
            } else {
                payload.num = 1;
                payload.flag = false;
            }
            arr.push(payload);
            state.shopcarArr = arr;
            window.localStorage.setItem('shopcarArr', JSON.stringify(state.shopcarArr))
            return { ...state }
        case SAVEINDEX:
            let arr1 = [...state.classifyArr]
            state.rightArr = arr1[payload].info as any
            window.localStorage.setItem('rightArr', JSON.stringify(state.rightArr))
            return { ...state }
        case CHECKONE:
            let arrS = [...state.shopcarArr]
            arrS.forEach((item, index) => {
                if (item.id === payload) {
                    item.flag = !item.flag
                }
            })
            let states = arrS.every(item => item.flag)
            state.checkbox = states
            state.shopcarArr = arrS
            allmoney();
            window.localStorage.setItem('shopcarArr', JSON.stringify(state.shopcarArr))
            window.localStorage.setItem('checkbox', JSON.stringify(state.checkbox))
            return { ...state }
        case CHECKALL:
            let arre = [...state.shopcarArr]
            let dates = payload
            arre.forEach((item, index) => {
                item.flag = payload
            })
            state.checkbox = dates
            state.shopcarArr = arre
            allmoney();
            window.localStorage.setItem('shopcarArr', JSON.stringify(state.shopcarArr))
            window.localStorage.setItem('checkbox', JSON.stringify(state.checkbox))
            return { ...state }
        case ADDNUM:
            let addarr = [...state.shopcarArr];
            addarr.forEach((item, index) => {
                if (item.id === payload) {
                    item.num++
                }
            })
            state.shopcarArr = addarr;
            allmoney();
            window.localStorage.setItem('shopcarArr', JSON.stringify(state.shopcarArr))
            return { ...state }
        case REDUCENUM:
            let addarr1 = [...state.shopcarArr];
            addarr1.forEach((item, index) => {
                if (item.id === payload) {
                    if (item.num == 1) return;
                    item.num--
                }
            })
            state.shopcarArr = addarr1;
            allmoney();
            window.localStorage.setItem('shopcarArr', JSON.stringify(state.shopcarArr))
            return { ...state }
        case CHANGECHECK:
            let arrs = [...state.shopcarArr]
            arrs.forEach((item, index) => {
                if (item.flag) {
                    item.flag = false
                } else {
                    item.flag = true
                }
            })
            state.shopcarArr = arrs
            allmoney();
            window.localStorage.setItem('shopcarArr', JSON.stringify(state.shopcarArr))
            return { ...state }

        case ADDADDRESS:
            let addaDD = [...state.addressArr];
            addaDD.push(payload);
            state.addressArr = addaDD;
            window.localStorage.setItem('addressArr', JSON.stringify(state.addressArr))
            return { ...state }
            case DELADDRESS:
                let addDEL = [...state.addressArr];
                addDEL.forEach((item,index)=>{
                    if(item.id===payload){
                        addDEL.splice(index, 1) 
                    }
                });
                state.addressArr = addDEL;
                window.localStorage.setItem('addressArr', JSON.stringify(state.addressArr))
                return { ...state }
                case EDITADDRESS:
                let editArr = [...state.addressArr];
                editArr.forEach((item,index)=>{
                    if(item.id===payload.id){
                       Object.assign(item, payload)
                    }
                });
                state.addressArr = editArr;
                window.localStorage.setItem('addressArr', JSON.stringify(state.addressArr))
                return { ...state }
        default:
            return state
    }
}


const store = legacy_createStore(reducer, applyMiddleware(thunk, logger))
export default store;